<?php


namespace app\admin\controller;

use app\common\logic\StorehouseLogic;
use think\Db;
use think\Validate;
/**
 *
 * Class Storehouse
 * @package app\admin\controller
 */
class Storehouse extends Base
{
    /**
     * 仓库列表
     */
    public function storehouse_list(){

        $select_info = input('post.');
        $region_id = input('region_id', 0);
        $city_id = input('city_id', 0);
        $key_word = input('key_word', '');
        $rent_status = input('rent_status', 0);

        $rule = [
            'region_id' => 'number',
            'city_id' => 'number',
        ];

        $msg = [
            'region_id.require' => '区域格式错误',
            'city_id.number' => '城市格式错误',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($select_info);
        if (!$result) {
//            return ['status' => '-1', 'msg' => $validate->getError()];
            $this->returnMsg(-1, $validate->getError(), []);
        }

        $house_logic = new StorehouseLogic();
        $rs = $house_logic->store_house_list($region_id, $city_id, 2,$key_word,$rent_status);

        $this->returnMsg('1', '', $rs);
    }

    /**
     * 仓库详情
     */
    public function storehouse_msg()
    {
        $house_id = input('house_id', 0);
        $house_logic = new StorehouseLogic();
        $store_house = $house_logic->get_storehouse_msg($house_id);
        $this->returnMsg('1', '', $store_house);
    }

    /**
     * 添加仓库数据
     */
    public function add_storehouse(){


        $get_data = $this->body_data;//I('.');
//        var_dump($get_data);
        $storehouse_data = $get_data['storehouse'];
        $banner_data = $get_data['banner'];
        $landmark_data = $get_data['landmark'];
        $imgs = $get_data['imgs'];
        $items = $get_data['items'];


        $rule = [
            'house_name' => 'require|length:1,50',
            'icon' => 'require|length:1,200',
            'platform_img'=> 'require|length:1,200',
            'head_img'=> 'require|length:1,200',
        ];


        $msg = [
            'house_name.require' => '请输入仓库名字',
            'house_name.length' => '请输入1-50字的仓库名字',
            'icon.require' => '请上传图标',
            'icon.length' => '输入图标过长',
            'platform_img.require' => '请上传平面图',
            'platform_img.length' => '平面图地址格式不对',
            'head_img.require' => '请上传顶部平面图',
            'head_img.length' => '顶部平面图地址格式不对',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($storehouse_data);

        if (!$result) {
            $this->returnMsg(-1, $validate->getError(), []);
        }

        $names = $storehouse_data['house_name'];
        $have_db = Db::name('storehouse')->where(['house_name' => $names,'status'=>1])->find();
        if (!empty($have_db)) {
            $this->returnMsg(-1, '已有相同的仓库名字', []);
        }

        Db::startTrans();
        try{
            $house_id = Db::name('storehouse')->insertGetId($storehouse_data);
            if ($house_id) {
                foreach ($banner_data as $key => $value) {
                    if (!$value)
                        continue;

                    Db::name('storehouse_banner')->insert(['storehouse_id' => $house_id, 'img_url' => $value,'types'=>1]);
                }

                foreach ($imgs as $key => $value) {
                    if (!$value)
                        continue;
                    Db::name('storehouse_banner')->insert(['storehouse_id' => $house_id, 'img_url' => $value,'types'=>2]);
                }

                //特色
                foreach ($items as $key => $value) {
                    if (!$value)
                        continue;
                    Db::name('store_item')->insert(['store_id' => $house_id, 'item_id' => $value]);
                }

                foreach ($landmark_data as $key => $value) {
                    if (!$value)
                        continue;

                    $value['storehouse_id'] = $house_id;
                    Db::name('storehouse_landmark')->insert($value);
                }
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->returnMsg(-1, '添加失败'.$e->getMessage(), []);
        }



        $this->returnMsg(1, '添加成功', []);

    }


    /**
     * 添加仓库数据
     */
    public function edit_storehouse()
    {

        $get_data = $this->body_data;//I('.');
        $storehouse_data = $get_data['storehouse'];
        $banner_data = $get_data['banner'];
        $landmark_data = $get_data['landmark'];
        $imgs = $get_data['imgs'];
        $items = $get_data['items'];

        $rule = [
            'house_id'=>'require',
            'house_name' => 'require|length:1,50',
            'icon' => 'require|length:1,200',
            'platform_img'=> 'require|length:1,200',
            'head_img'=> 'require|length:1,200',
        ];

        $msg = [
            'house_id.require' => '请输入仓库编号',
            'house_name.require' => '请输入仓库名字',
            'house_name.length' => '请输入1-50字的仓库名字',
            'icon.require' => '请输入图标',
            'icon.length' => '输入图标过长',
            'platform_img.require' => '请输入平面图',
            'platform_img.length' => '平面图地址格式不对',
            'head_img.require' => '请输入顶部平面图',
            'head_img.length' => '顶部平面图地址格式不对',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($storehouse_data);
        if (!$result) {
//            return ['status' => '-1', 'msg' => $validate->getError()];
            $this->returnMsg(-1, $validate->getError(), []);
        }

        $names = $storehouse_data['house_name'];
        $house_id = $storehouse_data['house_id'];

        $house_db = Db::name('storehouse')->where(['house_id' => $house_id,'status'=>1])->find();
        if(!$house_db) {
            $this->returnMsg(-1, '没有找到仓库数据', []);
        }

//        $have_db = Db::name('storehouse')->where(['house_name' => $names])->find();
        $have_db = Db::name('storehouse')->where(['house_name' => $names,'status'=>1, 'house_id' => ['neq', $house_id]])->find();
        if (!empty($have_db)) {
            $this->returnMsg(1, '已有相同的仓库名字', []);
        }

        Db::startTrans();
        try {
            if ($house_id) {
                Db::name('storehouse')->where(['house_id' => $house_id])->update($storehouse_data);
                Db::name('storehouse_banner')->where(['storehouse_id' => $house_id])->delete();
                Db::name('storehouse_landmark')->where(['storehouse_id' => $house_id])->delete();
                Db::name('store_item')->where(['store_id' => $house_id])->delete();

                //banner
                foreach ($banner_data as $key => $value) {
                    if (!$value)
                        continue;

                    Db::name('storehouse_banner')->insert(['storehouse_id' => $house_id, 'img_url' => $value, 'types' => 1]);
                }

                //底部图片
                foreach ($imgs as $key => $value) {
                    if (!$value)
                        continue;
                    Db::name('storehouse_banner')->insert(['storehouse_id' => $house_id, 'img_url' => $value, 'types' => 2]);
                }

                //特色
                foreach ($items as $key => $value) {
                    if (!$value)
                        continue;
                    Db::name('store_item')->insert(['store_id' => $house_id, 'item_id' => $value]);
                }

                //地标
                foreach ($landmark_data as $key => $value) {
                    if (!$value)
                        continue;
                    $add_value = ['land_name' => $value['land_name'],
                        'land_ename' => $value['land_ename'],
                        'distance' => $value['distance'],
                        'storehouse_id' => $value['storehouse_id'],
                        'land_id' => $value['land_id'],
                        'storehouse_id' => $house_id];

                    Db::name('storehouse_landmark')->insert($add_value);
                }
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->returnMsg(-1, '修改失败'.$e->getMessage(), []);
        }


        $this->returnMsg(1, '修改成功', []);

    }

    /**
     * 删除仓库
     */
    public function delete_storehouse(){

        $house_id = input('house_id/d',0);

        Db::startTrans();
        try {
//            Db::name('storehouse')->where(['house_id' => $house_id])->delete();
//            Db::name('storehouse_banner')->where(['storehouse_id' => $house_id])->delete();
//            Db::name('storehouse_landmark')->where(['storehouse_id' => $house_id])->delete();
            Db::name('storehouse')->where(['house_id' => $house_id])->update(['status'=>2]);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->returnMsg(1, '删除失败', []);
        }

        $this->returnMsg(1, '删除成功', []);
    }


}